/**
 * Created by zyx on 2017/9/18.
 */
angular.module('module.auth')
    .controller('ForgotPwdController', ForgotPwdController);
ForgotPwdController.$inject = ['AuthService'];

// 忘记密码
function ForgotPwdController(AuthService) {

    var vm = this;
    vm.sureBtn = sureBtn; // 模态框
    vm.cancelBtn = cancelBtn; // 模态框
    vm.checkPhone = checkPhone;
    vm.getCode = getCode;

    function sureBtn() {
        vm.messageVisible = false;
    }

    function cancelBtn() {
        vm.messageVisible = false;
    }

    $("#delete").click(function () {
        document.getElementById("phone").value = "";
    });

    // 确认手机号码
    function checkPhone() {
        var judge = /^1(3|4|5|7|8)\d{9}$/;
        if (vm.phone == "" || vm.phone == undefined) {
            $("#code").attr("disabled");
        } else if (!(judge.test(vm.phone))) {
            $("#code").attr("disabled");
        } else {
            AuthService.getExit(vm.phone).$promise.then(function (data) {
                if (data.result == "该手机号已注册") {
                    $("#code").removeAttr("disabled");
                } else {
                    $("#code").attr("disabled");
                    vm.messageVisible = true;
                    vm.message = "该手机号暂未注册！";
                    //alert("该手机号暂未注册！")
                }
            }, function (error) {
                //console.log(error)
            })
        }
    }

    // 获取验证码
    function getCode() {
        var thisBtn = document.getElementById("code");
        AuthService.getExit(vm.phone).$promise.then(function (data) {
            if (data.result != "该手机号已注册") {
                vm.messageVisible = true;
                vm.message = "该手机号暂未注册！";
            } else {
                sendCode(thisBtn);
                AuthService.getFgPwdCode(vm.phone).$promise.then(function (data) {
                    if (data.result == "获取短信成功") {
                        $("#next").removeAttr("disabled");
                    }
                })
            }
        }, function (error) {

        });
    }
}
